package com.nuaa.community.controller;

import com.nuaa.community.service.DateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;

/**
 * ClassName: dataController
 * Package: com.nuaa.community.controller
 * Description:
 */
@Controller
public class dataController {

    @Autowired
    private DateService dataservice;

    /**
     * 统计页面
     * @return
     */
    @PostMapping(path = "/data")
    @GetMapping(path = "/data")
    public String getDataPage() {
        return "site/admin/data";
    }

    /**
     * 统计网站uv
     * @param start
     * @param end
     * @param model
     * @return
     */
    @PostMapping(path = "data/uv")
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model){
        long uv = dataservice.calculateUV(start, end);
        model.addAttribute("uvResult", uv);
        model.addAttribute("uvStartDate", start);
        model.addAttribute("uvEndDate", end);
        return "forward:/data";
    }

    /**
     * 统计活跃用户
     * @param start
     * @param end
     * @param model
     * @return
     */
    @PostMapping(path = "data/dau")
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model){
        long dau = dataservice.calculateDAU(start, end);
        model.addAttribute("dauResult", dau);
        model.addAttribute("dauStartDate", start);
        model.addAttribute("dauEndDate", end);
        return "forward:/data";
    }

}
